<template>
  <template v-if="type === '1'">
    <a-button
      v-for="(item, index) in props.btnList"
      :key="index"
      v-permission="getPermission(batchBtnPermission, item.type)"
      :disabled="getDisabled(item)"
      @click="btnClick(item.func, item.type)"
      >{{ item.name }}</a-button
    >
  </template>
  <template v-if="type === '2' && props.btnList.length > 0">
    <a-button
      v-for="(item, index) in props.btnList"
      :key="item.name + index"
      v-permission="getPermission(rowBtnPermission, item.type)"
      style="margin-right: 24px"
      type="text"
      @click="btnClick(item.func, item.type)"
      >{{ item.name }}</a-button
    >
  </template>
</template>

<script lang="ts" setup>
  import { ref, defineProps, defineEmits } from 'vue';

  const props = defineProps({
    btnList: {
      type: Array<any>,
      default: () => [],
    },
    /**
     * 集群类型，1：列表上方操作，2：别表操作列按钮
     */
    type: {
      type: String,
      default: '1',
    },
    otherParams: {
      type: Object,
      default: null,
    },
  });
  // 操作列按钮权限
  const rowBtnPermission = ref({
    '1': ['procurement:require-receive:doUpdateRemark'], // 编辑
    '2': [], // 详情
    '3': ['procurement:require-receive:doConfirm'], // 确认
    '4': ['procurement:require-receive:delete'], // 删除
  });
  // 批量操作按钮权限
  const batchBtnPermission = ref({
    '1': ['procurement:require-receive:doUpdateRemarkList'], // 批量编辑
    '2': ['procurement:require-receive:doImport'], // Excel导入
    '3': ['procurement:require-receive:doCloseList'], // 手动关闭
    '4': ['procurement:require-receive:doPurchaseList'], // 指定是否采购
    '5': ['procurement:require-receive:doCloseImport'], // 导入需要手工关闭的需求
    '6': ['procurement:require-receive:doImport'], // Excel导入
    '7': ['procurement:require-receive:doConfirmList'], // 批量确认
    '8': ['procurement:require-receive:deleteList'], // 批量删除
    '9': ['procurement:require-receive:doConfirmAll'], // 全部确认
    '10': ['procurement:require-receive:doCheckList'], // 重新校验
    '11': ['procurement:require-receive:export'], // 导出
    '12': ['procurement:require-receive:doOpenList'], // 恢复手动关闭
  });
  const getPermission = (obj: any, type: string) => {
    if (props.otherParams.functionType === 1) {
      return obj[type] ? obj[type] : [];
    }
    if (obj[type] && obj[type].length > 0) {
      const arr = obj[type].map((item: string) => {
        return `${item}-lx`;
      });
      return arr;
    }
    return [];
  };
  const emit = defineEmits(['result']);
  const getDisabled = (item: any) => {
    if (item.isDisabled) {
      return !(props.otherParams.selection.length > 0);
    }
    return false;
  };
  const btnClick = async (func: (params?: any) => void, type: string) => {
    await func(props.otherParams);

    emit('result', type, props.otherParams);
  };
</script>

<style lang="less" scoped>
  .row-btn-container {
    :deep(.arco-btn) {
      height: 16px;
      padding: 0 !important;
    }
  }
</style>
